//
//  HMTabBatController.swift
//  swift微博项目
//
//  Created by huodian on 16/9/22.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

class HMTabBatController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let hmresign = HMMaintabbar()
        
        
        hmresign.closuer = {[unowned self] in
            
            print("按钮被点击了")
            
            print(self)
            
            let componseView = HMComponseView()
            
            componseView.show(target: self)
            
            
            
        }
        
        
        self.setValue(hmresign, forKey: "tabBar")
        
      // addChildViewController()
        
       
        loadTheViewcontroller()
        
    }
    
    func loadTheViewcontroller()
    {
        
        print("进来了没")
        self.addChildviewcontrollerToMain(title: "首页", image: "tabbar_home", vc: HMHomeViewController(),index: 0)
        

        self.addChildviewcontrollerToMain(title: "信息", image:"tabbar_message_center", vc: HMMessageViewController(),index: 1)
        
         self.addChildviewcontrollerToMain(title: "发现", image: "tabbar_discover", vc: HMDIscoverViewController(),index: 2)
        
        self.addChildviewcontrollerToMain(title: "我", image: "tabbar_profile", vc: HMMeViewController(),index: 3)
        
       
        
        
        
    }

    func addChildviewcontrollerToMain(title:String,image:String,vc:UIViewController,index:Int){
        
        
        vc.navigationItem.title = title
        
        vc.tabBarItem.title = title
        
        vc.tabBarItem.tag = index
        
        vc.tabBarItem.image = UIImage(named: image)
        
        vc.tabBarItem.selectedImage = UIImage(named: image + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal).withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        
        
        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(3, 0, -3, 0)
        
        
        let nav = HMNavgationController(rootViewController:vc)
        
       
        
         self.addChildViewController(nav) 
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        var index: Int = 0
        
        
        for subView in tabBar.subviews {
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                for childView in subView.subviews {
                    
                    if childView.isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                        
                        
                        if item.tag == index {
                            
                            childView.transform =  CGAffineTransform.init(scaleX: 0.4, y: 0.4)
                            
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { 
                                  childView.transform = CGAffineTransform.init(scaleX: 1, y: 1)
                                }, completion: { (_) in
                                    
                                    print("动画设置成功")
                                    
                            })
                            
                            
                            
                        }
                        
                       index += 1
                    }
                    
                }
                
                
            }
            
            
        }
        
        
    }
    
    
    
}
